package cn.seapon.talker.edit;

import java.net.URLEncoder;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.seapon.talker.MainActivity;
import cn.seapon.talker.R;
import cn.seapon.talker.infocarrier.SharedItems;
import cn.seapon.talker.net.HttpRequest;

/**
 * @author 作者 E-mail:
 * @version 创建时间：2015年1月8日 上午10:15:50 类说明
 */
public class EditEmailActivity extends Activity {
	private static final String TAG = "||__EditEmailActivity__||";
	private EditText et_value;
	private Button save_btn;
	private static final String EDIT_MAIL_URL = "http://1.lichtung.sinaapp.com/appserver/appservice.php?request_code=2003";
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Toast.makeText(EditEmailActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
				Log.d(TAG, "修改成功");
				save_btn.setText("返回主页面");
				save_btn.setOnClickListener(backlistener);
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.save_btn:
				final String newvalue = et_value.getText().toString().trim();
				final String url = EDIT_MAIL_URL + "&new_email="
						+ URLEncoder.encode(newvalue) + "&userid="
						+ SharedItems.userInformation.getAccount();
				Log.d(TAG, url);
				Thread thread = new Thread(){

					@Override
					public void run() {
						String retString = HttpRequest.doGetRequest(url);
						Message message = Message.obtain();
						if ("success".equals(retString)) {
							message.what = 1;
							SharedItems.userInformation.setEmail(newvalue);
							handler.sendMessage(message);
						}
					}
					
				};
				thread.start();
				break;

			default:
				break;
			}
		}
	};

	private OnClickListener backlistener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			EditEmailActivity.this.finish();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_edit_email);
		et_value = (EditText) findViewById(R.id.et_value);
		save_btn = (Button) findViewById(R.id.save_btn);

		save_btn.setOnClickListener(listener);
	}

}
